#pragma once 

#include <iostream>
#include <string>
#include <vector>
#include <fcntl.h>

//设置一个sock成为非阻塞
void set_non_block(int sock)
{
    int fl = fcntl(sock, F_GETFL);
    if (fl < 0)
    {
        std::cerr << "fcntl failed" << std::endl;
        return;
    }
    fcntl(sock, F_SETFL, fl | O_NONBLOCK);
}

void split_segment(std::string& inbuffer, std::vector<std::string>& tokens, const std::string& separator)
{
    while(true)
    {
        std::cout << "inbuffer: " << inbuffer << std::endl;
        auto pos = inbuffer.find(separator);
        if (pos == std::string::npos)
        {
            break;
        }
        std::string sub = inbuffer.substr(0, pos);
        tokens.push_back(sub);
        inbuffer.erase(0, pos + separator.size());
    }
}

bool no_serializable(std::string& seg, std::string& data1, std::string& data2, std::string& op)
{
    std::string ops = "+-*/%";
    size_t pos;
    for (char ch : ops)
    {
       if ((pos = seg.find(ch)) != std::string::npos)
       {
           data1 = seg.substr(0, pos);
           op = seg.substr(pos, 1);
           data2 = seg.substr(pos + op.size());
           return true;
       }
    }
    return false;
}
